/**   
 * @Title: AutoUpdate.java 
 * @Package com.esun.services 
 * @Description: TODO(启动时检查是否有更新) 
 * @author Miko baoz@500wan.com   
 * @date 2011-4-1 上午10:27:10  
 */
package com.esun.util.update;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.esun.config.Config;
import com.esun.config.DistributionChannels;
import com.esun.util.log.EDebug;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;

/**
 * @ClassName: AutoUpdate
 * @Description: TODO(启动时检查是否有更新)
 * @author Miko baoz@500wan.com
 * @date 2011-4-1 上午10:27:10
 * 
 */
public class AutoUpdate {

	private static final String CHANNEL = "channel";
	private static final String APPVERSION = "version";
	private static final String PLATFORM = "platform";
	private static final String PRODUCT = "product";
	private static final String ACTION = "event";
	private static final String DEBUG = "debug";

	private String udpateAdd;

	public AutoUpdate(Context context) {
		this.context = context;
	}

	private Context context;

	public boolean checkUpdate() {

		String serverString = getServerUpdateInfo();

		EDebug.o(this, "********* check update server responce : " + serverString + "  ******************");
		EDebug.o(this, "*********    serverString    is  \"\"  : " + serverString.equals("") + "  ******************");
		EDebug.o(this, "*********    serverString    legth is  : " + serverString.length() + "  ******************");
		EDebug.o(this, serverString.split("|"));
		EDebug.o(this, ">>>>======================================================<<<");
		EDebug.o(this, "0".split("|"));
		EDebug.o(this, "*********    serverString    is     0  : " + serverString.substring(0, 1).equals("0") + "  ******************");
		try {
			if ((serverString.equals("105") || serverString.substring(0, 1).equals("0"))) {
				return false;
			} else {
				setUdpateAdd(serverString);
				return true;
			}

		} catch (Exception e) {
			return false;
		}

	}

	public String getUdpateAdd() {
		return udpateAdd;
	}

	public void setUdpateAdd(String udpateAdd) {
		this.udpateAdd = udpateAdd;
	}

	private String getServerUpdateInfo() {

		HttpPost httpRequest = new HttpPost(Config.UPDATE_ADD);
		String serverStr = "105";

		try {

			httpRequest.setEntity(new UrlEncodedFormEntity(getPostInfo(), HTTP.UTF_8));
			HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
			EDebug.o(AutoUpdate.this, "auto run is " + httpResponse.toString());

			serverStr = EntityUtils.toString(httpResponse.getEntity());

		} catch (Exception e) {
			serverStr = "105";
		}

		return serverStr;
	}

	private List<NameValuePair> getPostInfo() throws NameNotFoundException {

		List<NameValuePair> params = new ArrayList<NameValuePair>();

		// 渠道号
		params.add(new BasicNameValuePair(CHANNEL, DistributionChannels.QUDAO));

		// 软件版本号
		String versionName = context.getPackageManager().getPackageInfo(Config.APP_PACKAGE_INFO, 0).versionName;
		params.add(new BasicNameValuePair(APPVERSION, versionName));
		EDebug.o(this, versionName);

		// 平台
		params.add(new BasicNameValuePair(PLATFORM, "android"));

		// 产品代号
		params.add(new BasicNameValuePair(PRODUCT, "rabbit"));

		// 服务器请求动作
		params.add(new BasicNameValuePair(ACTION, "2"));

		// 调试功能
		if (!Config.UPDATE_DEBUG.equals(Config.NUM1)) {
			params.add(new BasicNameValuePair(DEBUG, Config.UPDATE_DEBUG));
		}
		EDebug.o(AutoUpdate.this, "auto run is " + params.toString());
		return params;
	}
}
